home *** CD-ROM | disk | FTP | other *** search
/ Interactive Web Graphics with Shout 3D / Interactive Web Graphics With Shout 3D.iso / mac / Code / Chapter10 / TargetViewpoint.java < prev    next >
Text File  |  2000-07-17  |  3KB  |  97 lines

  1.  
  2. package custom_nodes;
  3.  
  4. import  shout3d.core.*;
  5. import  shout3d.math.*;
  6.  
  7. /**
  8.  * TargetViewpoint
  9.  * 
  10.  */
  11.  
  12. public class TargetViewpoint extends Viewpoint  {
  13.  
  14.    float[] origin = {0.0f, 0.0f, 0.0f};
  15.    
  16.    final public FloatField heading = new FloatField(this, "heading",  Field.ANY, 0);
  17.    final public FloatField pitch = new FloatField(this, "pitch", Field.ANY, 0);
  18.    final public FloatField distance = new FloatField(this, "distance", Field.NON_NEGATIVE_FLOAT, 10);   
  19.    final public FloatArrayField center = new FloatArrayField(this, "center", Field.COORD3, origin);
  20.  
  21.    Quaternion q = new Quaternion();                      
  22.    float[] eulers = {0.0f, 0.0f, 0.0f};
  23.  
  24.  
  25.    public TargetViewpoint(){
  26.       this(null);
  27.    }
  28.    
  29.    public TargetViewpoint(Shout3DViewer viewer){
  30.  
  31.       super(viewer);
  32.       
  33.       heading.addFieldObserver(this, null);
  34.       pitch.addFieldObserver(this, null);
  35.       distance.addFieldObserver(this, null);
  36.       center.addFieldObserver(this,null);
  37.  
  38.       setViewpoint(); 
  39.    }
  40.  
  41.    
  42.    protected void finalize() throws Throwable { 
  43.  
  44.       heading.removeFieldObserver(this);
  45.       pitch.removeFieldObserver(this);
  46.       distance.removeFieldObserver(this);
  47.       center.removeFieldObserver(this);
  48.  
  49.       super.finalize();
  50.    }
  51.    
  52.    
  53.    public void onFieldChange(Field theField, Object userData) {
  54.  
  55.       if ( theField == heading || theField == pitch || theField == distance || theField == center) {
  56.          setViewpoint();
  57.       }
  58.       else {
  59.          // call super class--Viewpoint
  60.          super.onFieldChange(theField, userData);
  61.       }
  62.    }
  63.    
  64.  
  65.    public void setViewpoint() {
  66.    
  67.       //set quaternion to current heading and pitch
  68.       eulers[0] = heading.getValue();
  69.       eulers[1] = pitch.getValue();
  70.       q.setEulers(eulers);
  71.  
  72.       //set distance vector
  73.       //and rotate it
  74.       if ( distance.getValue() < 0.0f) {
  75.          distance.setValue(0.0f);
  76.       }
  77.       float[] vector = {0.0f, 0.0f, distance.getValue()};
  78.       q.xform(vector);
  79.  
  80.  
  81.       //translate vector relative
  82.       //to target center
  83.         vector[0] = vector[0] + center.getValue()[0];
  84.       vector[1] = vector[1] + center.getValue()[1];
  85.       vector[2] = vector[2] + center.getValue()[2];
  86.    
  87.  
  88.       //set Viewpoint rotation and
  89.       //translate to end of vector
  90.       float[] axisAngle = new float[4];
  91.       q.getAxisAngle(axisAngle);
  92.       orientation.setValue(axisAngle);
  93.       position.setValue(vector);
  94.  
  95.    }
  96. }
  97.